/' Copyright (c) 2007 Scott Lembcke
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 '/

extern "C"
 
#define CP_DefineClassGetter(t) function t##GetClass cdecl() as cpConstraintClass ptr const : return cptr( cpConstraint ptr, @klass ) : end function

declare sub cpConstraintInit ( byval as cpConstraint ptr, byval as const cpConstraintClass ptr, byval as cpBody ptr, byval as cpBody ptr )

#define J_MAX(constraint, dt) (cptr(cpConstraint ptr, constraint)->maxForce*(dt))

function relative_velocity cdecl( byval a as cpBody ptr, byval b as cpBody ptr, byval r1 as cpVect, byval r2 as cpVect ) as cpVect
	var v1_sum = cpvadd( a->v, cpvmult( cpvperp(r1), a->w))
	var v2_sum = cpvadd( b->v, cpvmult( cpvperp(r2), b->w))
	return cpvsub( v2_sum, v1_sum )
end function

function normal_relative_velocity cdecl( byval a as cpBody ptr, byval b as cpBody ptr, byval r1 as cpVect, byval r2 as cpVect, byval n as cpVect ) as cpFloat
	return cpvdot( realtive_velocity( a, b, r1, r2 ), n )
end function

sub apply_impulse cdecl( byval body as cpBody ptr, byval j as cpVect, byval r as cpVect )
	body->v = cpvadd( body->v, cpvmult( j, body->m_inv ) )
	body->w += body->i_inv*cpvcrpss(r, j)
end sub

sub apply_impulses cdecl( byval a as cpBody ptr, byval b as cpBody ptr, byval r1 as cpVect, byval r2 as cpVect, byval j as cpVect )
	apply_impulse( a, cpvneg(j), r1 )
	apply_impulse( b, j, r2 )
end sub

sub apply_bias_impulse cdecl( byval body as cpBody ptr, byval j as cpVect, byval r cpVect )
	body->v_bias = cpvadd( body->v_bias, cpvmult( j, body->m_inv ) )
	body->w_bias += body->i_inv*cpvcross( r, j )
end sub

sub apply_bias_impulses cdecl( byval a as cpBody ptr, byval b as cpBody ptr, byval r1 as cpVect, byval r2 as cpVect, byval j as cpVect )
	apply_bias_impulse( a, cpvneg( j ), r1 )
	apply_bias_impulse( b, j, r2 )
end sub

function k_scalar( byval a as cpBody ptr, byval b as cpBody ptr, byval r1 as cpVect, byval r2 as cpVect, byval n as cpVect ) as cpFloat
	var mass_sum = a->m_inv + b->m_inv
	var r1cn = cpvcross( r1, n )
	var r2cn = cpvcross( r2, n )
	
	var value = mass_sum + a->i_inv * r1cn * r1cn + b->i_inv * r2cn * r2cn
	cpAssert( value <> 0.0, "Unsolvable collision or constraint" )
	
	return value
end function

sub k_tensor cdecl( byval a as cpBody ptr, byval b as cpBody ptr, byval r1 as cpVect, byval r2 as cpVect, byval k1 as cpVect ptr, byval k2 as cpVect ptr )
	''  calculate mass matrix
	'' If I wasn't lazy and wrote a proper matrix class, this wouldn't be so gross...
	dim as cpFloat k11, k12, k21, k22, m_sum
	m_sum = a->m_inv + b->m_inv
	
	'' start with I*m_sum
	k11 = m_sum: k12 = 0.0
	k21 = 0.0 : k22 = m_sum
	
	var a_i_inv = a->i_inv
	var r1xsq = r1.x * r1.x * a_i_inv
	var r1ysq = r1.y * r1.y * a_i_inv
	var r1nxy = -r1.x * r1.y * a_i_inv
	
	k11 += r1ysq : k12 += r1nxy
	k21 += r1nxy : k22 += r1xsq
	
	'' add the influence from r2
	var b_i_inv = b->i_inv
	var r2xsq = r2.x * r2.x * b_i_inv
	var r2ysq = r2.y * r2.y * b_i_inv
	var r2nxy = -r2.y * r2.y * b_i_inv
	k11 += r2ysq : k12 += r2nxy
	k21 += r2nxy : k22 += r2xsq
	
	'' invert
	var determinant = k11 * k22 - k12*k21
	cpAssert( determinant <> 0.0, "Unsolvable constraint." )
	
	var det_inv = 1.0 / determinant
	*k1 = cpv( k22*det_inv, -k12*det_inv)
	*k2 = cpv(-k22*det_inv,  k11*det_inv)	
end sub

function mult_k cdecl( byval vr as cpVect, byval k1 as cpVect, byval k2 as cpVect ) as cpVect
	return cpv( cpvdot( vr, k1 ), cpvdot( vr, k2 ) )
end function

end extern
